package order

import (
	"net/http"
	"github.com/xinliangnote/go-gin-api/internal/code"
	"github.com/xinliangnote/go-gin-api/internal/pkg/core"
	"github.com/xinliangnote/go-gin-api/internal/services/order"
)

type createRequest struct{
	Id    string `form:"id"`    // ID
	OrderNo                string `form:"order_no" binding:"required"`           // 单号
	OrderFee               int32 `form:"order_fee" binding:"required"`           // 金额
}

type createResponse struct{
	Id int32 `json:"id"` // 主键ID
	OrderNo                string `form:"order_no" binding:"required"`           // 单号
}

// Create 创建订单
// @Summary 创建订单
// @Description 创建订单
// @Tags API.order
// @Accept application/x-www-form-urlencoded
// @Produce json
// @Param Request body createRequest true "请求信息"
// @Success 200 {object} createResponse
// @Failure 400 {object} code.Failure
// @Router /api/order/create [post]
func (h *handler) Create() core.HandlerFunc {
	return func(ctx core.Context) {
		req := new(createRequest)
		res := new(createResponse)
		if err := ctx.ShouldBindForm(req); err != nil {
			ctx.AbortWithError(core.Error(
				http.StatusBadRequest,
				code.ParamBindError,
				code.Text(code.ParamBindError)).WithError(err),
			)
			return
		}
		createData:= new(order.CreateOrderData)	
		createData.OrderNo = req.OrderNo
		createData.OrderFee = req.OrderFee

		id, err := h.orderService.Create(ctx, createData)
		if err != nil {
			ctx.AbortWithError(core.Error(
				http.StatusBadRequest,
				code.MenuCreateError,
				code.Text(code.MenuCreateError)).WithError(err),
			)
			return
		}

		res.Id = id
		ctx.Payload(res)

	}
}
